#include "defaultstartcamerainfoactor.h"
#include "datatypedefine.h"
#include "vectorproperties.h"
#include "stringproperty.h"
#include "numberproperty.h"
#include "map.h"

namespace VrDataCore
{
	DefaultStartCameraInfoActor::DefaultStartCameraInfoActor()
	{
		setClassName(CLASS_CAMERA_START_INFO);
		_defaultAttitudeList.push_back(osg::Vec3(500,500,500));
		_defaultAttitudeList.push_back(osg::Vec3(0,0,0));
		_defaultAttitudeList.push_back(osg::Vec3(0,0,1));
		_roamAttitudeList.push_back(osg::Vec3(47.0f,27.0f,1.6f));
		_roamAttitudeList.push_back(osg::Vec3(26.0f,27.0f,1.6f));
		_roamAttitudeList.push_back(osg::Vec3(0,0,1));
	}

	DefaultStartCameraInfoActor::~DefaultStartCameraInfoActor()
	{}

	void DefaultStartCameraInfoActor::buildPropertyMap()
	{
		addProperty(new Vec3fListProperty<DefaultStartCameraInfoActor>(XML_ELEMENT_DATATYPE_VEC3FLIST,
			"Camrea Attitude","",this,&DefaultStartCameraInfoActor::setRoamStartInfo,
			&DefaultStartCameraInfoActor::getRoamStartInfo,"",""));
		addProperty(new Vec3fListProperty<DefaultStartCameraInfoActor>(XML_ELEMENT_DATATYPE_VEC3FLIST,
			"MainManipulator Attitude","",this,&DefaultStartCameraInfoActor::setDefaultStartInfo,
			&DefaultStartCameraInfoActor::getDefaultStartInfo,"",""));
	}

	void DefaultStartCameraInfoActor::setRoamStartInfo(QList<osg::Vec3> data)
	{
		_roamAttitudeList = data;
	}

	QList<osg::Vec3> DefaultStartCameraInfoActor::getRoamStartInfo()
	{
		return _roamAttitudeList;
	}

	void DefaultStartCameraInfoActor::setDefaultStartInfo(QList<osg::Vec3> data)
	{
		_defaultAttitudeList = data;
	}

	QList<osg::Vec3> DefaultStartCameraInfoActor::getDefaultStartInfo()
	{
		return _defaultAttitudeList;
	}
	///////////////////////////////////////////////////////////////////////////////
	WithCamerasSetting::WithCamerasSetting()
	{
		setClassName(CLASS_WITH_CAMERA_SETTING);
	}

	WithCamerasSetting::~WithCamerasSetting()
	{}

	void WithCamerasSetting::buildPropertyMap()
	{
		addProperty(new StringListProperty<WithCamerasSetting>(XML_ELEMENT_DATATYPE_STRINGLIST,
			"WithCamerasSetting","",this,&WithCamerasSetting::setCameraSetting,
			&WithCamerasSetting::getCameraSetting,"",""));
		addProperty(new StringProperty<WithCamerasSetting>(XML_ELEMENT_DATATYPE_STRING,
			"HostActorId","",this,&WithCamerasSetting::setHostActor,
			&WithCamerasSetting::getHostActor,"",""));
	}

	void WithCamerasSetting::setCameraSetting(QStringList data)
	{
		_idList = data;
	}

	QStringList WithCamerasSetting::getCameraSetting()
	{
		return _idList;
	}

	void WithCamerasSetting::addCameraSetting(CoreBase::ActorBase* a)
	{
		if(!_settingList.contains(a))
		{
			_settingList.push_back(a);
			_idList.push_back(a->getId());
		}
		getBelongMap()->addActor(*a);
	}

	void WithCamerasSetting::removeCameraSetting(CoreBase::ActorBase* a)
	{
		if(_settingList.contains(a))
		{
			_settingList.removeOne(a);
			_idList.removeOne(a->getId());
		}
		getBelongMap()->removeActor(*a);
	}

	QList<CoreBase::ActorBase*> WithCamerasSetting::getCameraSettingList()
	{
		return _settingList;
	}

	QList<osg::Vec3> WithCamerasSetting::getSetting(const QString& name)
	{
		for(int i=0;i<_settingList.size();++i)
		{
			if(_settingList.at(i)->getName() == name)
			{
				return dynamic_cast<CamerasSetting*>(_settingList[i])->getSetting();
			}
		}
		return QList<osg::Vec3>();
	}

	void WithCamerasSetting::onMapLoadEnd(CoreBase::Map* map)
	{
		QMap<QString,osg::ref_ptr<CoreBase::ActorBase>> allactors = map->getAllActors();
		QMap<QString,osg::ref_ptr<CoreBase::ActorBase>>::Iterator iter = allactors.begin();
		for(;iter!=allactors.end();++iter)
		{
			if(iter.value()->isInstanceOf(CLASS_CAMERA_SETTING))
			{
				if(_idList.contains(iter.value()->getId()))
				{
					addCameraSetting(dynamic_cast<NoTransformActor*>(iter.value().get()));
				}
			}
		}
	}

	void WithCamerasSetting::setHostActor(QString id)
	{
		_hostActorId = id;
	}

	QString WithCamerasSetting::getHostActor()
	{
		return _hostActorId;
	}
	///////////////////////////////////////////////////////////////////////////
	CamerasSetting::CamerasSetting():_nfr(NEARFARRATIO_VIEWER)
	{
		setClassName(CLASS_CAMERA_SETTING);
	}

	CamerasSetting::~CamerasSetting()
	{}

	void CamerasSetting::buildPropertyMap()
	{
		addProperty(new Vec3fListProperty<CamerasSetting>(XML_ELEMENT_DATATYPE_VEC3FLIST,
			"CamerasSetting","",this,&CamerasSetting::setSetting,
			&CamerasSetting::getSetting,"",""));
		addProperty(new StringProperty<CamerasSetting>(XML_ELEMENT_DATATYPE_STRING,
			"CamerasSetting Icon","",this,&CamerasSetting::setWithIcon,
			&CamerasSetting::getWithIcon,"",""));
		addProperty(new DoubleProperty<CamerasSetting>(XML_ELEMENT_DATATYPE_DOUBLE,
			"Camera Near Far Ratio","",this,&CamerasSetting::setWithCameraNearFarRatio,
			&CamerasSetting::getWithCameraNearFarRatio,"",""));
	}

	void CamerasSetting::setSetting(QList<osg::Vec3> data)
	{
		_setting = data;
	}

	QList<osg::Vec3> CamerasSetting::getSetting()
	{
		return _setting;
	}

	void CamerasSetting::setWithIcon(QString icon)
	{
		_icon = icon;
	}

	QString CamerasSetting::getWithIcon()
	{
		return _icon;
	}

	void CamerasSetting::setWithCameraNearFarRatio(double nfr)
	{
		_nfr  = nfr;
	}

	double CamerasSetting::getWithCameraNearFarRatio()
	{
		return _nfr;
	}
}